import {Component, OnInit} from '@angular/core';
import {AddGooTypeLevel2Component} from '../add-goo-type-level2.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {GooTypeLevel2} from '../../goo-type-level2.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {GooTypeLevel1Condition} from '../../../goo-type-level1/goo-type-level1.type';
import {isNullOrUndefined} from 'util';

@Component({
    selector: 'app-main-add-goo-type-level2',
    templateUrl: './main-add-goo-type-level2.component.html',
    styleUrls: ['./main-add-goo-type-level2.component.css']
})
export class MainAddGooTypeLevel2Component implements OnInit {

    errRes = new ErrRes();

    domain = new GooTypeLevel2();

    extend = {
        domain: {
            serLevel1: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '一级分类类型',
                change: (event) => {
                    console.log(event);
                }
            },
            default: () => {
                this.domain.serAudit = 'WAIT_SUBMIT';
                this.domain.serUsing = 'ENABLE';
            },
            serAudit: () => {
                return 'serAudit';
            },
        },
    };
    func = {
        save: {
            req: (event) => {
                Services.gooTypeLevel2.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.gooTypeLevel2.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public addGooTypeLevel2: AddGooTypeLevel2Component) {
        this.addGooTypeLevel2.main = this;
        // ng select
        this.domain.serOneId = null;
    }

    ngOnInit() {
        this.extend.domain.default();
        Services.gooTypeLevel1.list(new GooTypeLevel1Condition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.serLevel1.items = [];
                item.page.content.forEach(level1 => {
                    this.extend.domain.serLevel1.items.push({id: level1.uuid, name: level1.serTypeName});
                });
            }
        });
    }

}
